package com.netty.web.utils;

import org.apache.tomcat.util.codec.binary.Base64;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * @author zl
 * @version 1.0
 * @date 2021/10/22 10:46
 */

public class ImageUtils {

    private static final String DEFAULT_URL_ENCODING = "UTF-8";

    /***
     * <p>
     * description: base64字符串转图片
     * <p>
     * @see
     */
    public static void base64StringToImage(String base64String,String path) {
        try {
            byte[] bytes1 = Base64.decodeBase64(base64String);
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
            BufferedImage bi1 = ImageIO.read(bais);
            File f1 = new File(path);
            ImageIO.write(bi1, "png", f1);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /***
     * <p>
     * description:图片转base64字符串：
     * <p>
     * @see
     */
    public static String getImgStr(String imgFile) {
        // 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        InputStream in = null;
        byte[] data = null;
        // 读取图片字节数组
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new String(Base64.encodeBase64(data));
    }

    public static void main(String[] args) {
        //String ss = "";
        //base64StringToImage(ss.split(",")[1],"e:/image/1.png");
        for (int i = 0; i < 10000; i++) {
            System.out.println(i);
        }
    }

}
